$import("com.joypard.platform.base.appsystem.AppSystemDI");
/**
 * 选择应用系统组件 <br>
 * selectchange事件：只有选择被改变的时候触发
 */
K.def("com.joypard.platform.base.appsystem.AppSystemCompoent", {
	statics : {
		/**
		 * 创建一个选中应用的组件
		 */
		createSelectAppSystemPanel : function(config) {
			var options = {
				title : "应用系统",
				iconCls : "p-panel",
				width : 220,
				checkModel : "single",
				useDefSelect : true, // 是否启用默认
				defSelect : null, // 默认选中code
				defSelectIndex : false, // 默认选中索引
				defSelectId : false, // 默认选中id
				defSelectCode : false, // 默认选中code
				frame : true
			};
			K.extend(options, config);

			var appSystems = this.loadAllSystem();
			var root = {
				iconCls : "menu_home_101",
				leaf : false,
				text : "系统",
				children : appSystems
			};

			var systemTreePanel = new K.Ext.tree.CheckboxTree(options);
			if (systemTreePanel.hasListener("selectchange")) {
				systemTreePanel.getSelectionModel().on("selectionchange", function(selModel, node) {
					this.fireEvent("selectchange", node);
				}, systemTreePanel);
			}

			systemTreePanel.setRootNode(root);
			if (!systemTreePanel.renderTo && !systemTreePanel.rendered) { // 作为其他组件的子组件
				systemTreePanel.on("afterrender", this._onAppSystemTreeAfterrender, systemTreePanel);
			} else { // 直接渲染
				this._onAppSystemTreeAfterrender(systemTreePanel);
			}

			return systemTreePanel;
		},
		/**
		 * 加载所有应用系统数据JSON的
		 */
		loadAllSystem : function() {
			var appSystems = AppSystemDI.loadAllSystem();
			$.each(appSystems, function(i, sys) {
				// sys.checked = false;
				sys.text = sys.appName;
				sys.leaf = true;
				sys.iconCls = "menu_home_101";
			});
			return appSystems;
		},
		/**
		 * 树渲染后处理默认选中
		 */
		_onAppSystemTreeAfterrender : function(systemTreePanel) {
			var rootNode = systemTreePanel.getRootNode();
			rootNode.expand();

			var defSelectNode = null;
			systemTreePanel.defSelectCode = systemTreePanel.defSelectCode || systemTreePanel.defSelect;

			if (systemTreePanel.defSelectId !== false) {
				rootNode.eachChild(function(node) {
					if (node.id == systemTreePanel.defSelectId) {
						defSelectNode = node;
						return false;
					}
				});
			} else if (systemTreePanel.defSelectCode) {
				rootNode.eachChild(function(node) {
					if (node.appCode == systemTreePanel.defSelectCode) {
						defSelectNode = node;
						return false;
					}
				});
			} else if (systemTreePanel.defSelectIndex) {
				if (rootNode.hasChildNodes()) {
					systemTreePanel.defSelectIndex = systemTreePanel.defSelectIndex >= rootNode.childNodes.length ? rootNode.childNodes.length - 1
							: systemTreePanel.defSelectIndex < 0 ? 0 : systemTreePanel.defSelectIndex;
					defSelectNode = rootNode.childNodes[systemTreePanel.defSelectIndex];
				}
			} else if (systemTreePanel.useDefSelect) {
				defSelectNode = rootNode.childNodes[0];
			}

			if (defSelectNode) {
				systemTreePanel.fireEvent("click", defSelectNode);
			}
		}
	}
});
